package game.utils.net 
{
	import flash.events.EventDispatcher;
	import flash.net.NetConnection;
	import flash.net.registerClassAlias;
	import flash.net.Responder;
	import game.model.event.NetServiceEvent;
	/**
	 * ...
	 * @author Thot
	 */
	public class AMFService extends EventDispatcher
	{
		
		private var _responder:Responder;
		private var _connection:NetConnection;
		
		public function AMFService( p_gatewayURL:String ) 
		{
			init(p_gatewayURL);
		}
		
		private function init(p_gatewayURL:String):void 
		{
			_responder = new Responder( onDataReceived, onErrorReceived );
			_connection = new NetConnection();
			_connection.connect( p_gatewayURL );
		}
		
		public function registerClass( p_alias:String, p_class:Class ):void
		{
			registerClassAlias( p_alias, p_class );
		}
		
		public function call( p_service:String, p_params:Array ):void
		{
			_connection.call( p_service, _responder, p_params );
		}
		
		private function onDataReceived( p_obj:Object ):void
		{
			dispatchEvent( new NetServiceEvent(NetServiceEvent.RECEIVE_DATA, p_obj) );
		}
		
		private function onErrorReceived( p_obj:Object ):void
		{
			dispatchEvent( new NetServiceEvent(NetServiceEvent.NET_ERROR, p_obj) );
		}
		
	}

}